/*
 * Serum
 *
 * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
 */

Serum.Form.Module.Extra.MyPreferences = Ext.extend(Serum.Form.Module.Edit, {

    /**
     * Constructor
     */
    constructor: function(onSuccess) {
        Serum.Form.Module.Extra.MyPreferences.superclass.constructor.call(this, {}, {
            module:         'Admins',
            controller:     'Preferences',
            action:         'edit',
            saveAction:     'save'
        }, onSuccess);
    },

    /**
     * Returns the top toolbar
     */
    getTopToolbar: function() {
        return null;
    },

    /**
     * Loads form items
     *
     * @param   {Object}    fieldsList
     */
    load: function(fieldsList) {
        // Initializes request params
        var _params = {
            method: Serum.Selector.get().method({
                module:     this.module,
                controller: this.controller,
                action:     this.action
            }),
            requestId: Serum.Helper.Request.id()
        };

        // Ajax request to get fields list
        Serum.Ajax.request({
            scope:       this,
            params:     _params,
            onSuccess:  function(response) {
                            var jsonResponse = Ext.util.JSON.decode(response.responseText);

                            if (typeof jsonResponse['result']['schema'] !== 'undefined') {
                                this._build(jsonResponse['result'], fieldsList);
                            }
                        }
        });
    },

    /**
     * Private build
     *
     * @param   {Object}    response    Fields list
     * @param   {Object}    fieldsList
     */
    _build: function(response, fieldsList) {
        var schema          = response['schema'];
        var newField        = null;

        var fieldsetObj     = new Ext.form.FieldSet({
                                    title:          __('admins~form_fieldset_mypreferences'),
                                    collapsible:    true,
                                    autoHeight:     true,
                                    labelWidth:     this.defaultLabelWidth
                                });

        for (var i = 0; i < schema.length; i++) {
            newField                = {};
            newField['name']        = schema[i].key;
            newField['labelWidth']  = this.defaultLabelWidth;
            newField['fieldLabel']  = __('admins~form_label_preference_' + schema[i].key);
            newField['maxLength']   = 255;
            newField['allowBlank']  = false

            switch (schema[i].type) {
                case 'boolean':
                    newField['checked']   = (schema[i].value == 1 || schema[i].value == 'true');
                    newField['xtype']     = 'xcheckbox';
                    break;

                case 'string':
                    newField['value']     = schema[i].value;
                    newField['xtype']     = 'textfield';
                    break;

                case 'number':
                    newField['value']     = schema[i].value;
                    newField['xtype']     = 'numberfield';
                    break;
            }

            fieldsetObj.add(newField);
        }

        this.add(fieldsetObj);

        this.fireEvent('formbuilded');

        // Refreshes form
        this.doLayout();
        this.cleanErrors();
    },

    /**
     * Preloads form items (delete former items)
     *
     * @param   {Mixed} elementId
     */
    preload: function(elementId) {
        if (typeof this.items !== 'undefined') {
            var i;
            while(i = this.items.last()) {
                this.remove(i, true);
            }
        }
    }

});